{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Word Ladder Example Code\n",
    "\n",
    "Below is the Vertex and Graph class used for the Word Ladder example code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Vertex:\n",
    "    def __init__(self,key):\n",
    "        self.id = key\n",
    "        self.connectedTo = {}\n",
    "\n",
    "    def addNeighbor(self,nbr,weight=0):\n",
    "        self.connectedTo[nbr] = weight\n",
    "\n",
    "    def __str__(self):\n",
    "        return str(self.id) + ' connectedTo: ' + str([x.id for x in self.connectedTo])\n",
    "\n",
    "    def getConnections(self):\n",
    "        return self.connectedTo.keys()\n",
    "\n",
    "    def getId(self):\n",
    "        return self.id\n",
    "\n",
    "    def getWeight(self,nbr):\n",
    "        return self.connectedTo[nbr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class Graph:\n",
    "    def __init__(self):\n",
    "        self.vertList = {}\n",
    "        self.numVertices = 0\n",
    "\n",
    "    def addVertex(self,key):\n",
    "        self.numVertices = self.numVertices + 1\n",
    "        newVertex = Vertex(key)\n",
    "        self.vertList[key] = newVertex\n",
    "        return newVertex\n",
    "\n",
    "    def getVertex(self,n):\n",
    "        if n in self.vertList:\n",
    "            return self.vertList[n]\n",
    "        else:\n",
    "            return None\n",
    "\n",
    "    def __contains__(self,n):\n",
    "        return n in self.vertList\n",
    "\n",
    "    def addEdge(self,f,t,cost=0):\n",
    "        if f not in self.vertList:\n",
    "            nv = self.addVertex(f)\n",
    "        if t not in self.vertList:\n",
    "            nv = self.addVertex(t)\n",
    "        self.vertList[f].addNeighbor(self.vertList[t], cost)\n",
    "\n",
    "    def getVertices(self):\n",
    "        return self.vertList.keys()\n",
    "\n",
    "    def __iter__(self):\n",
    "        return iter(self.vertList.values())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Code for buildGraph function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def buildGraph(wordFile):\n",
    "    d = {}\n",
    "    g = Graph()\n",
    "    \n",
    "    wfile = open(wordFile,'r')\n",
    "    # create buckets of words that differ by one letter\n",
    "    for line in wfile:\n",
    "        print line\n",
    "        word = line[:-1]\n",
    "        print word\n",
    "        for i in range(len(word)):\n",
    "            bucket = word[:i] + '_' + word[i+1:]\n",
    "            if bucket in d:\n",
    "                d[bucket].append(word)\n",
    "            else:\n",
    "                d[bucket] = [word]\n",
    "    # add vertices and edges for words in the same bucket\n",
    "    for bucket in d.keys():\n",
    "        for word1 in d[bucket]:\n",
    "            for word2 in d[bucket]:\n",
    "                if word1 != word2:\n",
    "                    g.addEdge(word1,word2)\n",
    "    return g"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Please reference the video for full explanation!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
